package com.xxx.client.android.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.xxx.client.android.CustomApplication;
import com.xxx.client.android.R;
import com.xxx.client.android.adapter.PurpleAdapter;
import com.xxx.client.android.adapter.PurpleEntry;
import com.xxx.client.android.adapter.PurpleListener;
import com.xxx.client.android.adapter.SeparatedListAdapter;
import com.xxx.client.android.db.Database;
import com.xxx.client.android.db.DatabaseImpl;
import com.xxx.client.android.db.History;
import com.xxx.client.android.dialog.HistoryListLoadingDialog;
import com.xxx.client.android.util.Tool;

public class HistoryListActivity extends Activity {

	private ArrayList<History> historyList;
	private ListView mHistoryListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);

		mHistoryListView = (ListView) findViewById(R.id.Listview);
	}

	@Override
	protected void onResume() {
		fillArticleListView();
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// support for search key
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.history, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about_menu_item:
			Tool.about(this);
			break;
		case R.id.clear_menu_item:
			clear();
			break;
		default:
		}
		return super.onOptionsItemSelected(item);
	}

	public void clear() {
		Database database = new DatabaseImpl(this);
		int[] ids = new int[historyList.size()];
		int i = -1;
		for (History history : historyList) {
			ids[++i] = history.getHistoryId();
		}
		database.deleteHistory(ids);

		// separate adapters on one list
		SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
		PurpleAdapter mPurpleAdapter = new PurpleAdapter(this, false);
		ArrayList<PurpleEntry> mListEntry = new ArrayList<PurpleEntry>();
		mListEntry.add(new PurpleEntry(getString(R.string.history_fail)));
		// attach list data to adapters
		mPurpleAdapter.setList(mListEntry);
		// separate adapters on one list
		separatedAdapter
				.addSection(getString(R.string.history), mPurpleAdapter);

		mHistoryListView.setAdapter(separatedAdapter);
		mHistoryListView.setOnItemClickListener(mHistoryClickListener);
	}

	/**
	 * Launch this Activity from the outside
	 * 
	 * @param c
	 *            Activity from which HistoryListActivity should be started
	 */
	public static void launch(Activity c) {
		new HistoryListLoadingDialog(c, R.string.history_loading,
				R.string.history_fail).execute();
	}

	/**
	 * Fills ListView with clickable menu items
	 */
	@SuppressWarnings("unchecked")
	private void fillArticleListView() {
		CustomApplication.log("ArticleListActivity.fillArticleListView");

		historyList = (ArrayList<History>) getIntent().getSerializableExtra(
				"historyList");

		// separate adapters on one list
		SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
		PurpleAdapter mPurpleAdapter = new PurpleAdapter(this);
		ArrayList<PurpleEntry> mListEntry = new ArrayList<PurpleEntry>();
		History historyVO;
		int size = historyList.size();
		for (int i = 0; i < size; i++) {
			historyVO = historyList.get(i);
			final int historyType = historyVO.getHistoryType();
			final String historyUrl = historyVO.getHistoryUrl();
			mListEntry.add(new PurpleEntry(historyVO.getHistoryName(),
					new PurpleListener() {
						@Override
						public void performAction() {
							switch (historyType) {
							case History.INTERNAL:
								ArticleActivity.launch(
										HistoryListActivity.this,
										new Integer[] {
												Integer.valueOf(historyUrl),
												null, null });
								break;

							default:
								try {
									Tool.launchBrowser(
											HistoryListActivity.this,
											historyUrl);
								} catch (ActivityNotFoundException e) {
									Tool.messageBox(HistoryListActivity.this,
											getString(R.string.fail));
								}
								break;
							}
						}
					}));
		}
		// attach list data to adapters
		mPurpleAdapter.setList(mListEntry);
		// separate adapters on one list
		separatedAdapter
				.addSection(getString(R.string.history), mPurpleAdapter);

		mHistoryListView.setAdapter(separatedAdapter);
		mHistoryListView.setOnItemClickListener(mHistoryClickListener);
	}

	/**
	 * Launches menu actions
	 */
	private OnItemClickListener mHistoryClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view,
				int index, long time) {
			PurpleListener listener = ((PurpleEntry) adapterView.getAdapter()
					.getItem(index)).getListener();
			if (listener != null) {
				listener.performAction();
			}
		}

	};

}
